//
//  KDWeekCell.m
//  KDCalendarControl
//
//  Created by Keith Duncan on 07/07/2007.
//  Copyright 2007 thirty-three. All rights reserved.
//

#import "KDWeekCell.h"

#import "Amber/Amber.h"

NSRectArray CreateDayRectsForBounds(NSRect bounds) {
	NSRectArray rects = (NSRectArray)calloc(7, sizeof(NSRect));
	
	for (NSUInteger index = SUNDAY-1; index < SATURDAY; index++)
		rects[index] = DayRectForRowRect(bounds, index);
	
	return rects;
}

@interface KDWeekCell (PrivateDrawing)
- (void)_drawBezelWithFrame:(NSRect)frame inView:(NSView *)view;
- (void)_drawSelectionWithFrame:(NSRect)frame inView:(NSView *)view;
@end

@implementation KDWeekCell

@synthesize selectedDays=_selectedDays;
@synthesize selectedColor=_selectedColor, regularColor=_regularColor;

- (id)init {
	[super init];
	
	[self setFont:[NSFont systemFontOfSize:1]];
	
	_dayNames = [[NSArray alloc] initWithObjects:
				NSLocalizedString(@"Sunday", @"Sunday"),
				NSLocalizedString(@"Monday", @"Monday"),
				NSLocalizedString(@"Tuesday", @"Tuesday"),
				NSLocalizedString(@"Wednesday", @"Wednesday"),
				NSLocalizedString(@"Thursday", @"Thursday"),
				NSLocalizedString(@"Friday", @"Friday"),
				NSLocalizedString(@"Saturday", @"Saturday"), nil];
	
	_stringType = KDCapitalized;
	_titleLength = 1;
	
	_selectedDays = nil;
	
	_selectedColor = [[NSColor whiteColor] retain];
	_regularColor = [[NSColor colorWithCalibratedWhite:(89.0/255.0) alpha:1.0] retain];
	
	return self;
}

- (void)dealloc {
	[_dayNames release];
	
	[_selectedDays release];
	
	[_selectedColor release];
	[_regularColor release];
	
	[super dealloc];
}

- (BOOL)acceptsFirstResponder {
	return NO;
}

- (void)setStringType:(NSUInteger)type {
	_stringType = type;
}

- (void)setTitleLength:(NSUInteger)value {
	_titleLength = value;
}

- (NSRect)drawingRectForBounds:(NSRect)frame {
	frame.size.height -= 2;
	frame.origin.y += 1;
	return NSInsetRect(frame, 1, 0);
}

- (void)drawWithFrame:(NSRect)frame inView:(NSView *)view {
	[self _drawBezelWithFrame:frame inView:view];
	
	NSRect boundsRect = [self drawingRectForBounds:frame];
	if (_selectedDays != nil) [self _drawSelectionWithFrame:boundsRect inView:view];
	
	[super drawWithFrame:boundsRect inView:view];
}

- (void)_drawBezelWithFrame:(NSRect)frame inView:(NSView *)view {	
	CGFloat radius = NSHeight(frame)/2.0;
	NSBezierPath *backgroundPath = [NSBezierPath bezierPathWithRoundedRect:frame xRadius:radius yRadius:radius];
			
	NSGradient *shadowGradient = [[NSGradient alloc] initWithStartingColor:[NSColor controlShadowColor] endingColor:[NSColor whiteColor]];
	[shadowGradient drawInBezierPath:backgroundPath angle:-90];
	[shadowGradient release];
		
	NSRect contentRect = [self drawingRectForBounds:frame];
	CGFloat contentRadius = NSHeight(contentRect)/2.0;
	
	[[NSColor whiteColor] set];
	[[NSBezierPath bezierPathWithRoundedRect:contentRect xRadius:contentRadius yRadius:contentRadius] fill];
}

- (void)_drawSelectionWithFrame:(NSRect)frame inView:(NSView *)view {
	CGFloat contentRadius = NSHeight(frame)/2.0;
	NSRectArray dayRects = CreateDayRectsForBounds(frame);
	
	// Draw the selection
	NSBezierPath *selectionPath = [NSBezierPath bezierPath];
	
	NSRect workingRect = NSZeroRect;
	for (NSUInteger currentDay = SUNDAY; currentDay < SATURDAY+1; currentDay++) {
		NSRect currentRect = dayRects[currentDay-1];
		
		if ([_selectedDays containsIndex:currentDay]) workingRect = NSUnionRect(currentRect, workingRect);
		else if (!NSEqualRects(workingRect, NSZeroRect)) {
			[selectionPath appendBezierPathWithRoundedRect:workingRect xRadius:contentRadius yRadius:contentRadius];
			workingRect = NSZeroRect;
		}
	}
	
	if (!NSEqualRects(workingRect, NSZeroRect))
		[selectionPath appendBezierPathWithRoundedRect:workingRect xRadius:contentRadius yRadius:contentRadius];
	
	free(dayRects);
	
	if (![selectionPath isEmpty]) {
		[NSGraphicsContext saveGraphicsState];
		
		NSShadow *selectionPathShadow = [[NSShadow alloc] init];
		[selectionPathShadow setShadowOffset:NSMakeSize(0, -3)];
		[selectionPathShadow setShadowBlurRadius:2];
		
		[NSGraphicsContext saveGraphicsState];
		[selectionPathShadow set];
		
		if ([[view window] isKeyWindow]) {
			NSColor *topColor = [NSColor colorWithCalibratedRed:(25.0/255.0) green:(86.0/255.0) blue:(173.0/255.0) alpha:1.0];
			NSColor *bottomColor = [NSColor colorWithCalibratedRed:(93.0/255.0) green:(148.0/255.0) blue:(214.0/255.0) alpha:1.0];
			
			NSGradient *gradient = [[NSGradient alloc] initWithStartingColor:topColor endingColor:bottomColor];
			[gradient drawInBezierPath:selectionPath angle:90];
			[gradient release];
		} else {
			NSGradient *gradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedWhite:(130.0/255.0) alpha:1.0] endingColor:[NSColor colorWithCalibratedWhite:(180.0/255.0) alpha:1.0]];
			[gradient drawInBezierPath:selectionPath angle:90];
			[gradient release];
		}
		
		[NSGraphicsContext restoreGraphicsState];
		[selectionPathShadow release];
		
#if 0
		[selectionPath setClip];
		
		[[NSColor colorWithCalibratedWhite:0.5 alpha:0.3] set];
		
		NSBezierPath *shine;
		NSAffineTransform *translate;
		
		translate = [NSAffineTransform transform];
		[translate translateXBy:0 yBy:((2.0/3.0) * NSHeight(frame))];
		
		shine = [[selectionPath copy] autorelease];
		[shine transformUsingAffineTransform:translate];
		[shine fill];
		
		
		translate = [NSAffineTransform transform];
		[translate translateXBy:0 yBy:-((8.0/9.0) * NSHeight(frame))];
		
		shine = [[selectionPath copy] autorelease];
		[shine transformUsingAffineTransform:translate];
		[shine fill];
#endif
		[NSGraphicsContext restoreGraphicsState];
	}
}

- (void)drawInteriorWithFrame:(NSRect)frame inView:(NSView *)view {
	NSShadow *selectedTextShadow = [[NSShadow alloc] init];
	[selectedTextShadow setShadowOffset:NSMakeSize(0, -2)];
	[selectedTextShadow setShadowBlurRadius:3.0];
	
	NSRectArray dayRects = CreateDayRectsForBounds(frame);
	
	for (NSUInteger currentDay = SUNDAY; currentDay < SATURDAY+1; currentDay++) {
		NSRect dayRect = dayRects[currentDay-1];
		
		[NSGraphicsContext saveGraphicsState];
		
		if ([_selectedDays containsIndex:currentDay]) {
			[selectedTextShadow set];
			[_selectedColor set];
		} else [_regularColor set];
		
		NSString *string = [_dayNames objectAtIndex:currentDay-1];
		NSUInteger currentLength = _titleLength < [string length] ? _titleLength : [string length];
		string = [string substringToIndex:currentLength];
		
		if (_stringType == KDCapitalized) string = [string capitalizedString];
		else if (_stringType == KDUpperCase) string = [string uppercaseString];
		else if (_stringType == KDLowerCase) string = [string lowercaseString];
		
		AKDrawStringAlignedInFrame(string, [self font], NSCenterTextAlignment, dayRect);
		
		[NSGraphicsContext restoreGraphicsState];
	}
	
	free(dayRects);
	
	[selectedTextShadow release];
}

@end
